//
//  HMMainViewController.m
//  G05爱鲜蜂
//
//  Created by 源 何 on 16/10/14.
//  Copyright © 2016年 何源. All rights reserved.
//

#import "HMMainViewController.h"
#import "HMNavigationController.h"
#import "HMHomeViewController.h"
#import "MarketViewController.h"
#import "HMShopViewController.h"
#import "HMMineViewController.h"
@interface HMMainViewController ()

@property (nonatomic, strong) HMHomeViewController *hVC;

@end

@implementation HMMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupChildControllers];

}


#pragma mark
#pragma mark -- 加载子控制器
- (void)setupChildControllers{
    // 添加子控制器
    
    
    _hVC = [[HMHomeViewController alloc] init];
    
    [self addChildViewController:_hVC title:@"首页" imageName:@"v2_home" index: 0];
    [self addChildViewController:[[MarketViewController alloc] init] title:@"闪电超市" imageName:@"v2_order" index: 1];
    
    [self addChildViewController:[[HMShopViewController alloc] init] title:@"购物车" imageName:@"shopCart" index: 2];
    [self addChildViewController:[[HMMineViewController alloc] init] title:@"我的" imageName:@"v2_my" index: 3];
    
   
}

/**
 添加子控制器

 @param childController 子控制器
 @param title           标题
 @param imageName       图片名称
 */
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title imageName:(NSString *)imageName index:(int)index{
    // 设置标题
    childController.tabBarItem.title = title;
    
    childController.tabBarItem.tag = index;
    
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} forState:UIControlStateNormal];
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} forState:UIControlStateSelected];
    
    // 设置图像
    childController.tabBarItem.image = [UIImage imageNamed:imageName];
    
    NSString *selectedImageName = [NSString stringWithFormat:@"%@_r", imageName];
    
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 添加子控制器
    HMNavigationController *nav = [[HMNavigationController alloc] initWithRootViewController:childController];
    
    [self addChildViewController:nav];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    int index = 0;

    // 遍历子视图
    for (UIView *obj in tabBar.subviews) {
        if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            //获取到UITabBarButton
            if (index == item.tag) {
                
                for (UIView *target in obj.subviews) {
                    
                    if ([target isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
                        
                        target.transform = CGAffineTransformMakeScale(0.4, 0.4);
                        
                        [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:5 options:0 animations:^{
                            target.transform = CGAffineTransformMakeScale(1, 1);
                        } completion:^(BOOL finished) {
                        }];
                    }
                }
            }
            index++;
        }
    }
}





@end
